<template>
    <div>
        <van-address-edit
            :area-list="areaList"
            save-button-text='保存地址'
            show-set-default
            @save="onSave"
            :is-saving="loading"
        />
    </div>
</template>

<script>
import areaList from './../../lib/area';
export default {
    name: 'addArea',
    data() {
        return {
            areaList,

            loading: false
        }
    },

    methods: {
        onSave(data) {
            this.loading = true;
            let getCode = (arr, name) => {
                for (let i in this.areaList[arr]) {
                    if (this.areaList[arr][i] === name) {
                        return i;
                    }
                }
                return '';
            }
            let params = {
                name: data.name,
                tel: data.tel,
                proId: getCode('province_list', data.province), //省份ID
                cityId: getCode('city_list', data.city), //城市ID
                districtId: getCode('county_list', data.county), //区县ID
                proName: data.province,
                cityName: data.city,
                districtName: data.county,
                address: data.addressDetail,
                defaultTag: Number(data.isDefault)
            }

            this.$http.post('/shop/userDeliAddr/front/addDeliAddr', params).then(res => {
                this.loading = false;
                if (res.status === 0) {
                    this.$toast.success('新增成功');
                    setTimeout(() => {
                        if (this.$route.query.type === 'select') { 
                            localStorage.setItem('selectAddrId', res.data);
                        }
                        this.$router.back();
                    }, 700)
                } else {
                    this.$toast.fail(res.message);
                }
            })
        },
       
    }
}
</script>

<style lang="scss" scoped>
@import './../../assets/default.scss';
/deep/ .van-address-edit__buttons{
    padding: 0;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
}
/deep/ .van-button--danger {
    background-color: #ff6700;
    border: 1px solid #ff6700;
    border-radius: 0;
    margin: 0;
}
/deep/ .van-switch--on{background: #ffffff;}
/deep/ .van-switch--on .van-switch__node{background: #ff6700; border: 2px solid #fff}
</style>